# Template version 2.0

SOURCE=main.cpp
VIEWER=viewer.cpp
# Name executable same as source file but without cpp extension
EXEC=bin/Radiosity
VIEWEREXEC=bin/Viewer
CC = g++
FLAGS = -g -Wall -Wno-unused-function
INCLUDE = -I/usr/share/include

# Bash is required for "make tar" and "make submit".
SHELL=/bin/bash


ifeq ($(shell "uname"),Darwin)
	LIBS = -framework GLUT -framework OpenGL -lglew
else # Linux
	LIBS = -lGLEW -lglut -lX11 -lGL -lGLU -lm -lstdc++
endif

# Some assignments in this class may use ImageMagick (www.imagemagick.org).
#LIBS += `Magick-config --ldflags --libs --cppflags`


# General rule to compile program:
$(EXEC): $(SOURCE) $(HEADERS)
#	@type -P cppcheck &> /dev/null && ( echo "Running cppcheck since it is installed."; cppcheck -q $(SOURCE) ) || exit 0

	$(CC) -o $(EXEC) $(FLAGS) $(INCLUDE) ./src/*.cpp ./src/ObjLoader/*.cpp ./src/Math/*.cpp ./src/Radiosity/*.cpp $(SOURCE) $(LIBS) 
	$(CC) -o $(VIEWEREXEC) $(FLAGS) $(INCLUDE) ./src/*.cpp ./src/ObjLoader/*.cpp ./src/Math/*.cpp ./src/Radiosity/*.cpp $(VIEWER) $(LIBS) 

.PHONY: clean clobber tar submit
# "make clean" removes executables
clean:
	rm -f $(EXEC)
# remove directories created by visual studio
	rm -rf Debug Release
# Visual Studio will rebuild the sdf file, and it is large, so delete it:
	rm -f *.sdf

# "make clobber" removes executables and Visual Studio stuff
clobber: clean
	rm -f *.sln *.suo *.vcxproj *.vcxproj.filters

# Information for "make submit"
COURSENAME=cs4611
COURSESEC=sec1
# Number of minutes before we warn about stale file to submit:
WARN_STALE_FILE=5
TAR_FILE_BASENAME=${USER}
TAR_FILE=${TAR_FILE_BASENAME}.tgz

tar:
	@${MAKE} -s clean
	@rm -f ${TAR_FILE}
#	@tar --transform 's,^,${TAR_FILE_BASENAME}/,' -czf ${TAR_FILE} *
	@tar -czf ${TAR_FILE} *
	@echo "======================================================="
	@tar --list -vzf ${TAR_FILE}
	@echo "======================================================="
	@echo -n "Size: "
	@du -h ${TAR_FILE} | awk '{ print $$1 }'
	@echo "The files above are stored in: ${TAR_FILE}"
	@echo
	@echo "Your program HAS NOT BEEN SUBMITTED YET. Run"
	@echo "\"make submit\" to submit it or submit via Canvas"
	@echo "(depending on the assignment instructions)."
	@echo
	@echo "Note: If you extract the files from ${TAR_FILE}, it will"
	@echo "extract the files into your CURRENT DIRECTORY and overwrite"
	@echo "the existing files in your current directory!"


submit:
	@[[ -r ${TAR_FILE} ]] || echo "ERROR: ${TAR_FILE} missing, run 'make tar' first.";
	@[[ -r ${TAR_FILE} ]] || exit 1;

	@[[ -x /usr/local/bin/submit ]] || echo 'ERROR: You must be on a Rekhi lab machine.';
	@[[ -x /usr/local/bin/submit ]] || exit 1;

	@echo "Going to submit ${TAR_FILE}. It contains:"
	@echo "======================================================="
	@tar --list -vzf ${TAR_FILE} 
	@echo "======================================================="

	@echo -n "Total size of ${TAR_FILE}: "
	@du -h ${TAR_FILE} | awk '{ print $$1 }'

	@echo -n "${TAR_FILE} was last modified "
	@echo -n $$(( $$(( `date +%s` - `date +%s -r ${TAR_FILE}` )) / 60 ));
	@echo " minutes ago."
	@echo "======================================================="

	@if [[ $$(( $$(( `date +%s` - `date +%s -r ${TAR_FILE}` )) / 60 )) -ge ${WARN_STALE_FILE} ]]; then \
	echo ;\
	echo "WARNING WARNING WARNING WARNING WARNING WARNING WARNING";\
	echo "${TAR_FILE} is ${WARN_STALE_FILE} or more minutes old."; \
	echo "Make sure it contains the latest copies of your files!."; \
	echo "If you recently made changes, re-run 'make tar' to"; \
	echo "create a fresh file."; \
	echo "WARNING WARNING WARNING WARNING WARNING WARNING WARNING"; \
	echo ;\
	fi;

	@echo "Press Ctrl+C to cancel, any other key to continue."
	@read

	@echo -n "Enter the name of this assignment: "; \
	read hwname; \
	echo submit ${COURSENAME}.${COURSESEC} $$hwname ${TAR_FILE}; \
	submit ${COURSENAME}.${COURSESEC} $$hwname ${TAR_FILE};
	@echo
	@echo
	@echo "SUBMITTING LATE?  If you submit your assignment"
	@echo "late, you must email the professor---otherwise"
	@echo "your submission might not be noticed!"
